const express = require('express')
const router = express.Router()
const connection = require('.././mysql')

//! 获取订单列表
router.get('/orderList', (req, res) => {
    const sql = `SELECT * FROM goods_order`

    connection.query(sql, (err, result) => {
        if (err) {
            return res.status(500).json({
                message: '数据库查询失败',
                err
            })
        }
        

        if(result.length >= 0){
            return res.send({
                status: 200,
                msg:'获取订单列表成功',
                data: result
            })
        }
        else{
            return res.send({
                status: 400,
                msg:'获取订单列表失败'
            })
        }
    })
}) 



//! 删除列表单个项 
router.post('/delOrderItem', (req, res) =>{
    const o_id = req.body.params.o_id

    console.log(o_id);
    const sql = `DELETE FROM goods_order WHERE o_id = ?`

    connection.query(sql, [o_id], (err, result) => {
        if (err) {
            return res.status(500).json({
                message: '数据库查询失败'
            })
        }

        console.log(result);
        
        if(result.affectedRows > 0){
            return res.send({
                status: 200,
                msg:'删除订单成功'
            })
        }else{
            return res.send({
                status: 400,
                msg:'删除订单失败'
            })
        }
    })
    // res.send('ok')
    
}) 


module.exports = router